package com.jd.dlc.autoconfigure;

import com.jd.dlc.cache.DCache;
import com.jd.dlc.config.JimDBLockConfig;
import com.jd.dlc.config.LimitConfig;
import com.jd.dlc.spi.SPIFactory;
import com.jd.dlc.spi.SPILoader;
import com.jd.dlc.spring.SpringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;

/**
 * @program: dlc
 * @ClassName DlcConfiguration
 * @description: Dlc tools integrate Spring
 * @author: sizegang
 **/
@Configuration
@EnableConfigurationProperties(DlcProperties.class)
@ConditionalOnProperty(prefix = DlcProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true")
@Import({JimDBLockConfig.class, LimitConfig.class})
public class DlcConfiguration {

    /**
     * Dlc configuration
     */
    private final DlcProperties dlcProperties;

    public DlcConfiguration(DlcProperties dlcProperties) {
        this.dlcProperties = dlcProperties;
    }

    @Bean
    public SpringUtils springUtils() {
        return new SpringUtils();
    }

    /**
     * SPI loader
     *
     * @return
     */
    @Bean
    public SPILoader spiLoader() {
        return new SPILoader();
    }

    /**
     * SPI get factory
     *
     * @param spiLoader
     * @return
     */
    @Bean
    public SPIFactory spiFactory(SPILoader spiLoader) {
        return new SPIFactory(spiLoader);
    }

    /**
     * DCache instantiation
     *
     * @return
     */
    @DependsOn(value = {"springUtils"})
    @Bean
    public DCache dCache() {
        return new DCache();
    }


}
